#!/bin/sh

# Author: ng

set -e

echo "Lua scripts will be stripped in order to reduce file sizes and to speed up execution."

LSTRIP=./tools/lstrip/lstrip
if ! [ -x $LSTRIP ]
then
    echo "lstrip not found, try to build it."
    if ! [ -r jni/lua/src ]
    then
        echo "lua sources not found!"
        exit 1
    fi
    cd tools/lstrip/
    make
    cd ../..
fi

cd DeviceRes
if [ -x Lua ]
then
    echo "Erasing previous Lua folder..."
    rm -fr Lua
fi

echo "Copying scripts from sources..."
cp -r ../jni/CorsixTH/Lua .
oldSize=$(du -khc  Lua | tail -1 | cut  -f1)

echo "Stripping..."
find Lua -name '*.lua' | grep -v 'Lua/languages' | while read p
do
    mv $p $p.tmp
    ../$LSTRIP $p.tmp > $p
    oldS=$(ls -l $p.tmp| cut -d' ' -f5)
    newS=$(ls -l $p| cut -d' ' -f5)
    if [ $newS -gt $oldS ]
    then
        echo "Problem with $p ($oldS -> $newS)!!!"
    fi
    rm $p.tmp
done

sleep 0.5
newSize=$(du -khc  Lua | tail -1 | cut  -f1)

echo "Done. Old size was $oldSize, stripped to $newSize."

